package com.bilin.coin.activity;

import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.utils.TextUtils;
import com.bilin.coin.R;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.network.RequestUtils;
import com.bilin.coin.network.callback.CommonCallBack;
import com.bilin.coin.utils.DialogUtils;
import com.bilin.coin.utils.TimerUtils;
import com.trello.rxlifecycle2.android.ActivityEvent;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by LG on 2018/4/19.
 */
@Route(path = "/confirm/AlertTradePassActivity")
public class AlertTradePassActivity extends BaseAppCompatActivity {

    @BindView(R.id.ll_protocol)
    LinearLayout ll_protocol;
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_pass)
    EditText et_pass;
    @BindView(R.id.et_pass_again)
    EditText et_pass_again;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.bt_send)
    TextView bt_send;
    @BindView(R.id.cb_select)
    CheckBox cb_select;
    @BindView(R.id.ll_select_address)
    LinearLayout ll_select_address;
    @BindView(R.id.v_select)
    View v_select;
    @BindView(R.id.ll_invite)
    LinearLayout ll_invite;
    @BindView(R.id.v_invite)
    View v_invite;
    @BindView(R.id.rl_pic_code)
    RelativeLayout rl_pic_code;
    @BindView(R.id.view_pic_code)
    View view_pic_code;

    @Override
    protected void initView() {
        setTitle("修改交易密码");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_alert_trade_pass;
    }

    @OnClick({ R.id.bt_send,R.id.bt_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_confirm:
                confirm();
                break;
            case R.id.bt_send:
                sendCode();
                break;
        }
    }

    private void confirm() {

        String pass = et_pass.getText().toString().trim();
        String pass_again = et_pass_again.getText().toString().trim();
        String code  = et_code.getText().toString().trim();
        if (TextUtils.isEmpty(pass)) {
            showToast("密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            showToast(getResources().getString(R.string.sms_code_empty));
            return;
        }
        if (!pass.equals(pass_again)) {
            showToast(getResources().getString(R.string.login_two_pwd_no));
            return;
        }
        DialogUtils.showDialogLoading(this);
        RequestUtils
                .post()
                .url("/api/user/changeSafeWord")
                .addParams("code", code)
                .addParams("pwd", pass)
                .build()
                .compose(this.<String>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new CommonCallBack() {

                    @Override
                    protected void onError(String errorMsg) {
                        showToast(errorMsg);
                        DialogUtils.dismissDialogLoading();
                    }

                    @Override
                    protected void onSuccess(String dataJson) {
                        DialogUtils.dismissDialogLoading();
                        showToast("操作成功");
                        finish();
                    }

                    @Override
                    protected void onCallBackError(Throwable e) {
                        showMsg(e);
                        DialogUtils.dismissDialogLoading();
                    }
                });

    }

    private void sendCode() {
      /*  String phone = et_phone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            showToast(getResources().getString(R.string.phone_empty));
            return;
        }*/
        DialogUtils.showDialogLoading(this);
        RequestUtils
                .post()
                .url("/api/user/sendInnerCode")
            /*    .addParams("mobile",phone)*/
                .build()
                .compose(this.<String>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new CommonCallBack() {

                    @Override
                    protected void onError(String errorMsg) {
                        showToast(errorMsg);
                        DialogUtils.dismissDialogLoading();
                    }

                    @Override
                    protected void onSuccess(String dataJson) {
                        TimerUtils.getInitialise().startTimer(bt_send, 120, getString(R.string.login_resend_code));
                        DialogUtils.dismissDialogLoading();
                    }

                    @Override
                    public void onCallBackError(Throwable e) {
                        DialogUtils.dismissDialogLoading();
                        showMsg(e);
                    }
                });
    }
}
